package com.aica.bo;

import com.aica.entity.PersonalSignature;
import com.aica.entity.PunchInRecord;
import com.aica.entity.Role;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 无用户密码信息
 * @author by
 * @date 2021/6/8 12:08
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserMessageBo implements Serializable {
    /**
     * 用户id
     */
    private Integer userId;
    /**
     * 微信开放平台用户id
     */
    private Integer openId;
    /**
     * 用户名
     */
    private String username;
    /**
     * 昵称
     */
    private String nickName;

    /**
     * 电话号码
     */
    private String telephone;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 头像图片路径
     */
    private String avatar;
    /**
     * 用户性别  男 女
     */
    private String gender;

    /**
     * 当前日期(新增，记录注册时间)
     */
    private Date registerTime;




    //-----------------

    /**
     * 个性签名
     */
    private String personalSignature;
    /**
     * 总计使用天数
     */
    private Integer totalUseDays ;
    /**
     * 连续使用天数
     */
    private Integer consecutiveDays ;

    //-----------------

    /**
     * 角色id集合
     */
    private List<Integer> roleIds;
    /**
     * 角色描述集合
     */
    private List<Role> roles;

    //-----------------

    /**
     * 用户打卡记录
     */
    List<PunchInRecord> punchInRecordList;
    //todo 其他用户信息，如关注点赞之类的属性

    /**
     * 将各字段值注入
     * @param userAndRoleBo user表与role表的信息
     * @param personalSignatureBean 个性签名信息
     */
    public UserMessageBo(
            @NotNull UserAndRoleBo userAndRoleBo,
            PersonalSignature personalSignatureBean,
            List<PunchInRecord> punchInRecordList){

        userId = userAndRoleBo.getUserId();
        openId = userAndRoleBo.getOpenId();
        username = userAndRoleBo.getUsername();
        nickName = userAndRoleBo.getNickName();
        telephone = userAndRoleBo.getTelephone();
        email =userAndRoleBo.getEmail();
        avatar = userAndRoleBo.getAvatar();
        gender = userAndRoleBo.getGender();
        registerTime = userAndRoleBo.getRegisterTime();
        //---------------
        roleIds = userAndRoleBo.getRoleIds();
        roles = userAndRoleBo.getRoles();
        //---------------
        refreshPersonalSignature(personalSignatureBean);
        //---------------
        refreshTotalUseDays(punchInRecordList);
        //---------------
        refreshConsecutiveDays(punchInRecordList);
        //---------------
        this.punchInRecordList = punchInRecordList;


    }

    /**
     * 设置个性签名
     */
    private void refreshPersonalSignature(PersonalSignature personalSignatureBean){
        if(personalSignatureBean==null){
            return;
        }
        personalSignature = personalSignatureBean.getContent();
    }


        /**
         * 计算总计使用天数
         * @param punchInRecordList 打卡记录
         */
    private void refreshTotalUseDays(List<PunchInRecord> punchInRecordList){
        if(punchInRecordList==null){
            totalUseDays = 0;
            return;
        }
        totalUseDays = punchInRecordList.size();
    }

    /**
     * 计算连续使用天数
     * @param punchInRecordList 打卡记录
     */
    private void refreshConsecutiveDays(List<PunchInRecord> punchInRecordList){
        if(punchInRecordList==null || punchInRecordList.size()==0){
            consecutiveDays = 0;
            return;
        }

        consecutiveDays = 1;
        PunchInRecord tempPunchInRecord;

        Date tempDate = punchInRecordList.get(punchInRecordList.size()-1).getCurrentDateTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(tempDate);
        //从后往前遍历，获得最近连续天数
        for (int i = punchInRecordList.size()-2; i >=0; i--) {
            calendar.add(Calendar.DATE, -1);
            tempDate = calendar.getTime();
            tempPunchInRecord = punchInRecordList.get(i);
            if(tempDate.equals(tempPunchInRecord.getCurrentDateTime())){
                ++consecutiveDays;
            }else{
                break;
            }
        }
    }

//    protected static Format format = new SimpleDateFormat("yyyy-MM-dd");
//    public static void main(String[] args) {
//
//        try {
//            Date tempDate = (Date) format.parseObject("2021-01-05");
//            Date temp;
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(tempDate);
//
//            List<Date> pList = new ArrayList<>();
//            pList.add((Date) format.parseObject("2021-01-08"));
//            pList.add((Date) format.parseObject("2021-01-07"));
//            pList.add((Date) format.parseObject("2021-01-01"));
//            pList.add((Date) format.parseObject("2021-01-02"));
//            pList.add((Date) format.parseObject("2021-01-03"));
//            pList.add((Date) format.parseObject("2021-01-04"));
//            int j = 1;
//            for (int i = 5; i >=0; i--) {
//                calendar.add(Calendar.DATE, -1);
//                tempDate = calendar.getTime();
//                temp = pList.get(i);
//                if(tempDate.equals(temp)){
//                    ++j;
//                }else{
//                    break;
//                }
//            }
//            System.out.println(j);
//
//
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
//
//    }
}
